#include "musiclistitem.h"
#include "qdebug.h"
#include "ui_musiclistitem.h"
#include <QTime>

MusicListItem::MusicListItem(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MusicListItem)
    ,isLike(false)
{
    ui->setupUi(this);

    //设置QLabel字体格式
    QFont font;
    font.setFamily("Microsoft YaHei"); // 字体名（可换成“微软雅黑”、“SimSun”等）
    font.setPixelSize(12);             // 字体大小（像素）
    font.setBold(false);                // 是否加粗
    ui->musicSongTimeLabel->setFont(font);
    ui->musicNameLabel->setFont(font);
    ui->musicAlbumnLabel->setFont(font);
    ui->musicSingerLabel->setFont(font);
}

MusicListItem::~MusicListItem()
{
    delete ui;
}

void MusicListItem::setMusicName(const QString &name)
{
    QFontMetrics metrics(ui->musicNameLabel->font());
    QString elidedText = metrics.elidedText(name, Qt::ElideRight, ui->musicNameLabel->width());
    ui->musicNameLabel->setText(elidedText);
}

void MusicListItem::setMusicSinger(const QString &singer)
{
    QFontMetrics metrics(ui->musicSingerLabel->font());
    QString elidedText = metrics.elidedText(singer, Qt::ElideRight, ui->musicSingerLabel->width());
    ui->musicSingerLabel->setText(elidedText);
}

void MusicListItem::setMusicAlbum(const QString &album)
{
    QFontMetrics metrics(ui->musicAlbumnLabel->font());
    QString elidedText = metrics.elidedText(album, Qt::ElideRight, ui->musicAlbumnLabel->width());
    ui->musicAlbumnLabel->setText(elidedText);
}

void MusicListItem::setMusicSongTime(qint64 time_)
{
    QString show_time=QTime(0, 0).addSecs(time_/1000).toString(QString::fromLatin1("mm:ss"));
    ui->musicSongTimeLabel->setText(show_time);
}

void MusicListItem::enterEvent(QEvent *event)
{
    (void)event;
    setStyleSheet("background-color: #EFEFEF");
}

void MusicListItem::leaveEvent(QEvent *event)
{
    (void)event;
    setStyleSheet("");
}

void MusicListItem::setLikeMusic(const bool islike)
{
    this->isLike = islike;
    if(this->isLike){
        ui->likeBtn->setIcon(QIcon(":images/xihuan.png"));
    }else{
        ui->likeBtn->setIcon(QIcon(":images/buxihuan.png"));
    }
}

void MusicListItem::on_likeBtn_clicked()
{
    isLike = !isLike;
    setLikeMusic(isLike);
    //发送信号通知commonpage
    emit isLikeChanged(isLike);
}

